<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible="visible"
    @close="closeForm"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="85px"
    >
      <el-form-item
        label="用户名"
        prop="username"
        :class="{ 'is-required': !dataForm.userId }"
      >
        <el-input
          v-model="dataForm.username"
          placeholder="登录帐号"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="工号/学号"
        prop="personalNumber"
        :class="{ 'is-required': !dataForm.userId }"
      >
        <el-input
          v-model="dataForm.personalNumber"
          placeholder="工号/学号"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="真实姓名"
        prop="name"
        :class="{ 'is-required': !dataForm.userId }"
      >
        <el-input
          v-model="dataForm.name"
          placeholder="真实姓名"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="手机号码"
        prop="mobile"
        :class="{ 'is-required': !dataForm.userId }"
      >
        <el-input
          v-model="dataForm.mobile"
          placeholder="手机号码"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="dataForm.email"
          placeholder="邮箱"
          class="inputClass"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="type">
        <el-select v-model="dataForm.type" class="inputClass">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status" class="inputClass">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="memo" class="inputClass">
        <el-input
          class="inputClass"
          type="textarea"
          v-model="dataForm.memo"
          placeholder="备注"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentId">
        <department-select-tree
          @input="getDepId"
          :value="dataForm.departmentId"
          class="inputClass"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeForm">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import DepartmentSelectTree from './department-select-tree.vue'

export default {
  components: { DepartmentSelectTree },

  data() {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        userId: 0,
        username: '',
        email: '',
        mobile: '',
        status: 1,
        name: '',
        type: null,
        memo: '',
        departmentId: null,
        personalNumber: null
      },
      dataRule: {},
      typeList: [
        {
          id: 0,
          name: '管理员'
        },
        {
          id: 2,
          name: '职工'
        },
        {
          id: 3,
          name: '医护'
        },
        {
          id: 4,
          name: '学生'
        },
        {
          id: 5,
          name: '维修人员'
        },
        {
          id: 1,
          name: '教师'
        }
      ]
    }
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.userId) {
        this.$http({
          url: this.$http.adornUrl(
            `sys/visitor/getVisitorById/${this.dataForm.userId}`
          ),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 1) {
            this.dataForm = data.data
          }
        })
      }
    },
    closeForm() {
      this.visible = false
      this.dataForm = {}
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((vaild) => {
        if (vaild) {
          console.log('ok')
          console.log(this.dataForm)
          this.$http({
            url: this.$http.adornUrl(
              `sys/visitor/${
                !this.dataForm.userId ? 'addVisitor' : 'updateVisitor'
              }`
            ),
            method: `${!this.dataForm.userId ? 'post' : 'put'}`,
            data: this.$http.adornData({
              ...this.dataForm
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    getDepId(id) {
      this.dataForm.departmentId = id
    }
  }
}
</script>
<style lang="scss" scoped>
.inputClass {
  width: 400px;
}
</style>
